<?php

namespace App\Models;

use Validator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Admin extends Model
{
    //软删除
    use SoftDeletes;
    
    //允许哪些字段加入
    protected $fillable = ['username','password','nickname','email'];
    protected $dateFormat = 'U';
    protected $dates = ['create_at','updated_at','deleted_at'];  
    

    //登录处理
    public function login($data)
    {
        //验证失败后面不需要验证则加入bail
        $rule = [
            'username' => 'bail|required',
            'password' => 'required',
        ];
        $msg = [
            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空',
        ];
        //创建验证规则，make(验证的数据，验证的规则，错误信息)
        $validator = Validator::make($data,$rule,$msg);
        if ($validator->fails()) {
            //只返回多维数组第一条数据
            return $validator->errors()->first();
        }
        //first和find差不多
        $result = $this->where($data)->first();
        if ($result) {
            $sessionData = [
                'id' => $result['id'],
                'nickname' => $result['nickname'],
                'is_supper' => $result['is_supper'],
            ];
            //session 必须通过数组实现
            session(['admin' => $sessionData]);
            if ($result['status'] != 1){
                return '账号被禁用';
            }
            return 1;
        } else {
            return '用户名或者密码错误';
        }
    }

    //注册
    public function register ($data)
    {
        //same和confirmed
        $rule = [
            'username' => 'bail|required|unique:admins',
            'password' => 'required',
            'compass' => 'required|same:password',
            'nickname' => 'required',
            'email' => 'required|email|unique:admins'
        ];
        $msg = [
            'username.required' => '用户名不能为空',
            'username.unique' => '用户名已经存在',
            'password.required' => '密码不能为空',
            'compass.required' => '确认密码不能为空',
            'compass.same' => '两次密码不一致',
            'nickname.required' => '昵称不能为空',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.unique'=> '此邮箱已经被注册过'
        ];
        $validator =  Validator::make($data,$rule,$msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if ($result) {
            return 1;
        } else {
            return $result;
        }

    }

    //发送验证码
    public function getCode ($data)
    {
        $rule = [
            'email' => 'bail|required|email',
        ];
        $msg = [
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
        ];

        $validator = Validator::make($data,$rule,$msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->where('email',$data['email']) -> first();
        if($result){
            $code = mt_rand(1000,9999);
            session(['code' => $code]);
            $content = "您的验证码是".$code;
            $sendResult = mailto($data['email'],'获取验证码',$content);
            if ($sendResult) {
                return 1;
            } else{
                return '发送验证码失败';
            }
        } else {
            return "给邮箱尚未注册账号";
        }
    }

    //重置密码
    public function reset($data)
    {
        $rule = [
            'varify' => 'required'
        ];
        $msg = [
            'varify.required' => '验证码不能为空',
        ];

        $validator = Validator::make($data,$rule,$msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        if ($data['varify'] != session('code')) {
            return "验证码不正确";
        }
        $password = mt_rand(10000,99999);
        $adminInfo = $this->where('email',$data['email'])->first();
        $adminInfo->password = $password;
        $result = $adminInfo->save();
        if ($result) {
            $content = '您的用户名是'.$adminInfo['username'] . '<br>密码是' . $password;
            $sendResult = mailto($data['email'],'恭喜密码重置成功',$content);
            if ($sendResult) {
                return 1;
            } else {
                return "密码重置失败";
            }
        }else{
            return "密码重置失败";
        }
    }

    //管理员添加
    public function add ($data)
    {
        $rule = [
            'username' => 'bail|required|unique:admins',
            'password' => 'required',
            'nickname' => 'required',
            'email' => 'required|email|unique:admins',
        ];

        $msg = [
            'username.required' => "管理员姓名不能为空",
            'username.unique' => "管理员姓名已存在",
            'password.required' => "密码不能为空",
            'nickname.required' => "昵称不能为空",
            'email.email' => "邮箱格式不正确",
            'email.required' => "邮箱不能为空",
            'email.unique' => "邮箱已存在"
        ];

        $validator = Validator::make($data,$rule,$msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }

        $result = $this->create($data);

        if ($result) {
            return 1;
        } else {
            return "管理员添加失败";
        }
    }

    //管理员编辑
    public function  edit($data)
    {
        $rule = [
            'username' => 'bail|required',
            'password' => 'required',
            'nickname' => 'required',
            'email' => 'required|email',
        ];

        $msg = [
            'username.required' => '用户名不能为空',
            'username.unique' => '用户名已存在',
            'password.required' => '密码不能为空',
            'nickname.required' => '昵称不能为空',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '邮箱已存在'
        ];
        $validator = Validator::make($data,$rule,$msg);

        if ($validator->fails()) {
            return $validator->errors()->first();
        }

        $adminInfo = $this->find($data['id']);
        $adminInfo->username = $data['username'];
        $adminInfo->password = $data['password'];
        $adminInfo->nickname = $data['nickname'];
        $adminInfo->email = $data['email'];
        $result = $adminInfo->save();
        if ($result) {
            return 1;
        } else {
            return "编辑失败";
        }
        
    }
}
